#ifndef PAGOMENSUALMODULE_H
#define PAGOMENSUALMODULE_H

#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlRelationalTableModel>
#include <QTableView>
#include <QAction>
#include <QDebug>
#include <QHeaderView>
#include <QMessageBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QFormLayout>
#include <QColor>
#include <QRadioButton>
#include <QPainter>
#include <QSplitter>
#include <QVBoxLayout>
#include <QList>

#include "CustomToolbar.h"
#include "ClientModule.h"

class PagoMensualModule : public QMainWindow
{
    Q_OBJECT

public:
    explicit PagoMensualModule(QWidget *parent = 0);

signals:

public slots:
    void CleanPMList();
    void UpdatePMList();
    void CorrectViewOfModelPM();
    void UpdateClientList();
    void CorrectViewOfModelClient();
    //
    void AnioPasado();
    void AnioCorriente();
    //
    void EncontrarPersona();
    void FiltroPersona();
    void EncontrarAnio();
    void FiltroAnio();
    //
    void SeleccionarPM();

private:
    CustomModulesToolbar *pm_toolbar;

    QAction *accion_updateClientList;
    QAction *accion_updatePMList;

    QFormLayout *form_pm;

    QRadioButton *anioPasado;
    QRadioButton *anioCorriente;

    QRadioButton *encontrar_persona;
    QRadioButton *encontrar_anio;

    QLineEdit *campoBusqueda;
    QString filtroPM;
    QString cliente;
    QString anio;

    void UpdateClientListToolBar();
    void UpdatePMListToolBar();

    void CreateVerticalLayout();
};

class PMSqlTableModel : public QSqlRelationalTableModel
{
    Q_OBJECT
public:
    PMSqlTableModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase())
        : QSqlRelationalTableModel(parent,db) {;}
    QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const
    {
        if(role == Qt::ForegroundRole)
        {
            QModelIndex index2 = index.model()->index(index.row(), 5, index.parent()); // get index of "watched" column
            QModelIndex index3 = index.model()->index(index.row(), 4, index.parent()); // get index of "watched" column
            if (index.column() != 5) {
                if (index2.data(Qt::DisplayRole).toString() == "SI")
                    return QVariant(QColor(Qt::green));
                else {
                    if (index2.data(Qt::DisplayRole).toString() == "NO") {
                        if (index3.data(Qt::DisplayRole).toString() != "Bonificado") {
                            if (index3.data(Qt::DisplayRole).toString() != "0.00")
                                return QVariant(QColor(Qt::red));
                            else
                                return QVariant(QColor(Qt::black));
                        }
                        else
                            return QVariant(QColor(Qt::black));
                    }
                }
            }
            else {
                const QVariant value(data(index,Qt::DisplayRole));
                if (value.toString() == "SI") {
                   return QVariant(QColor(Qt::green));
                }
                else {
                    if (index2.data(Qt::DisplayRole).toString() == "NO") {
                        if (index3.data(Qt::DisplayRole).toString() != "Bonificado") {
                            if (index3.data(Qt::DisplayRole).toString() != "0.00")
                                return QVariant(QColor(Qt::red));
                            else
                                return QVariant(QColor(Qt::black));
                        }
                        else
                            return QVariant(QColor(Qt::black));
                    }
                }
            }
        }

        if (role == Qt::TextAlignmentRole) {
            int col = index.column();
            if (col == 0) {
                return QVariant(Qt::AlignVCenter | Qt::AlignHCenter);
            }
            if (col == 1 || col == 3) {
                return QVariant(Qt::AlignVCenter | Qt::AlignLeft);
            }
            if (col == 4) {
                return QVariant(Qt::AlignVCenter | Qt::AlignRight);
            }
        }

        if (role == Qt::FontRole) {
            return QVariant (QFont("Helvetica57-Condensed"));
        }

        return QSqlRelationalTableModel::data(index,role);
    }
};

#endif // PAGOMENSUALMODULE_H
